<?php
namespace Home\Controller;
use Common\Controller\MyInitController;
class RandomController extends MyInitController {
	public $callback_action	=	"";
    public function index($itemid = '',$type = '',$backurl = ''){
/*
itemnum    学生进行随机练习的答题量
itemid     题目编号
openid     微信用户标识
*/		
		
		
		
		
		/*=========定义变量=======*/
		$openid = $this->openid;
		$Question = M('questionbank');
		$Answer   = D('answer');  //需定义answer模型以进行正确的读写操作
		$isfromcollect = 0;
		/*======获取答题数量======*/		
		!strlen($openid) || $itemnum = $this->getItemNum($openid);
		
		
		
		
		
		if(empty($itemid)) {
            /*======读取随机试题======*/
			$count  =  $Question->count();
			$min    =  $Question->min('id');			
			$itemid =  rand($min,$min+$count);
        }elseif(is_string($itemid)){
			 /*======读取指定试题=====*/
			$isfromcollect = 1;
			if(!is_null($type)&&!is_null($backurl)) layout(false);
			$this->assign("backurl",$backurl);
			if($type == "wrong"){
				
			}
			
        }else{
			$this->error = L('_DATA_TYPE_INVALID_');
            return false;
		}
		$item   =  $Question->where("id=".$itemid)->find();
		
		/*=======错题集判断=======*/

		$tmp = compact("openid","itemid");
		$collect = $this->isCollect($tmp);
		if(!is_int($collect)) $this->assign('iscollect',1);
		$iscollect = (!is_int($collect))? 1:0;
		
		
		/*======设定模版变量======*/
		$item['question'] = nl2br(htmlspecialchars($item['question']));
		//$item['question'] = nl2br(mysql_real_escape_string($item['question']));
		//var_dump($item);
		//return false;
		//layout('Layout\layout');
		$this->assign('item',$item);
		$this->assign('comments',$this->getComment($tmp));
		
		$this->assign('iscollect',$iscollect);
		$this->assign('isfromcollect',$isfromcollect);
		$this->assign('starttime',date("Y-m-d H:i:s")); //.....
		//$this->assign('itemnum',$itemnum);
		$this->assign('openid',$openid);
		$this->display("Random/index");			
    }
	public function recordOption($answer,$openid=0,$itemid,$rightans,$usedtime){ //用于记录答题情况
		//I("post.record");
		

		$Answer = D('answer');
		if($Answer->create()){
			
			$answer = substr($answer,strlen($answer)-1,strlen($answer));
			
			$record['openid']    = $openid;
			//$record['time']   = date("Y-m-d H:i:s"); //设置数据表字段为timestamp类型
			$record['itemid']    = $itemid;
			$record['answer']    = $answer;
			$record['uesdtime']  = $usedtime;
			$record['right_lab'] = ($answer==$rightans)? 1:0;
		
			$Answer->add($record);			
		}else{
			$this->error($Answer->getError());			
		}
	}
	
	public function getRightAns(){
		/*=========判断是否通过ajax方式传输数据=======*/
		if(IS_AJAX){
			/*=========定义变量=======*/
            $itemid   = I('post.itemid');
            $openid   = I('post.openid');
            $answer   = I('post.answer');
            $starttime= I('post.starttime');
			$Question = M('questionbank');
			
			$usedtime = abs(strtotime(date("Y-m-d H:i:s"))-strtotime($starttime));
			/*=======获取正确答案=====*/	
			$ajaxreturn['rightans']     = $Question->where("id='{$itemid}'")->getField("rightans");
			$ajaxreturn['que_analysis'] = $Question->where("id='{$itemid}'")->getField("que_analysis");
			
			
			/*=======记录答题情况=====*/
			$this->recordOption($answer,$openid,$itemid,$ajaxreturn['rightans'],$usedtime);
		
			/*====返回答案解析到前台==*/
			$this->ajaxReturn($ajaxreturn);     			     
        }
		else $this->ajaxReturn('非法的请求方式'); 		
	}
	public function getItemNum($openid){
		$_Answer   = D('answer');
		$itemnum  =   $_Answer->where("openid='{$openid}'")->count();
		return $itemnum;
	}
	public function setCollect($data=''){
		/*====进行数据类型判断==*/
		if(empty($data)) {
            $data   =   I('post.');
        }elseif(is_object($data)){
            $data   =   get_object_vars($data);
        }
		if(empty($data) || !is_array($data)) {
            $this->error = L('_DATA_TYPE_INVALID_');
            return false;
        }
		/*====定义变量实例化==*/
		$_Collect = D("collect");
						
		$collectData["openid"]    = $data['openid'];
		$collectData["itemid"]    = $data['itemid'];
		
		
		/*====如未标记，将时间等信息保存到数据表==*/
		$collectData["right_lab"] = ($data['right_lab']=="正确")? 1:0;
		$collectData["time"] = date("Y-m-d H:i:s");
		
		$result = $_Collect->add($collectData);
		/*====向前台返回该用户错题集数量==*/
		if($result) {			
			$countnum = $_Collect->where("openid = '{$collectData["openid"]}' AND isdelete = 0")->count(); 
			$countnum = intval($countnum);
			if(IS_AJAX) $this->ajaxReturn($countnum);
			else return $countnum;
		}else{
			$this->error($_Collect->getError());
		}
	}
	public function isCollect($data=''){
		if(empty($data)) {
            $data   =   I('post.');
        }elseif(is_object($data)){
            $data   =   get_object_vars($data);
        }
		if(empty($data) || !is_array($data)) {
            $this->error = L('_DATA_TYPE_INVALID_');
            return false;
        }
		/*====定义变量实例化==*/
		$_Collect = D("collect");
					
		$collectData["openid"]    = $data['openid'];
		$collectData["itemid"]    = $data['itemid'];
		/*====判断是否存在该题目==*/
		$iscollect = $_Collect->where($collectData)->find();
		if(!is_null($iscollect)) {			
			return $iscollect["time"];
		}else return 0;
	}
	public function getComment($data = ""){		
		if(empty($data)) {
            $data   =   I('post.');
        }elseif(is_object($data)){
            $data   =   get_object_vars($data);
        }
		if(empty($data) || !is_array($data)) {
            $this->error = L('_DATA_TYPE_INVALID_');
            //return false;
        }
		$_Comment   =   D("comment");	
		$_Class     =   D("class");	
		
		$comments   = $_Comment->field("comment,openid,time,name,class")->where("itemid = '{$data['itemid']}' AND comment <> ''")->select();
		//$personinfo = $_Class->field("username,class")->where("openid = '{$comments['openid']}'")->find();
		//$ajaxreturn = array_merge($comments,$personinfo);

		
		
		return $comments;

	}
	public function setComment(){
		if(IS_AJAX){
			/*=========定义变量=======*/
            $itemid     = I('post.itemid');
            $openid     = I('post.openid');
            $comment   	= I('post.textcomment');
            $time       = date("Y-m-d H:i:s");
			
			$_Class   = M('class');
			$_Comment = D("comment");
			
			$info = $_Class->where("openid='{$openid}'")->field("username,class")->find();
			$info['name']	=	$info['username'];
			$tmp  =  compact("itemid", "openid","time","comment");
			
			$tmp  =  array_merge($tmp,$info);
			
			$result = $_Comment->add($tmp);
			
			
			
		
			if($result) {				
				$this->ajaxReturn($tmp);  
				
			}else{
				$this->error($_Collect->getError());
			}
			   			     
        }
		else $this->ajaxReturn('非法的请求方式'); 
	}
	public function getOptionNum(){
		if(IS_AJAX){
			/*=========定义变量=======*/
            $itemid     = I('post.itemid');
            $openid     = I('post.openid');
			
			$_Answer   = D('answer');
			$options = 'abcd';
			for($i=0;$i<4;$i++){
				$o =$options[$i];
				$option[$o] = $_Answer->where("itemid = $itemid AND answer = '{$o}'")->count();
			}
														
			if($option) {				
				$this->ajaxReturn($option);  				
			}else{
				$this->error($_Collect->getError());
			}
			   			     
        }
		else $this->ajaxReturn('非法的请求方式');
	}
	protected function doCallback($isSent, $to, $cc, $bcc, $subject, $body, $from)
    {
        if (!empty($this->callback_action) && is_callable($this->callback_action)) {
            $params = array($isSent, $to, $cc, $bcc, $subject, $body, $from);
            call_user_func_array($this->callback_action, $params);
        }
    }
	
	
}




?>